package com.apex.crm.exceptions;

import com.alibaba.fastjson.JSON;
import com.apex.crm.base.ResultInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author XiaoYang
 * 全局异常统一处理
 * @version 1.0
 * @program: crm
 * @Classname GlobalExceptionResolver
 * @description TODO
 * @date 2021/4/21 21:13
 */

@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,Object handler,Exception ex){
        if(ex instanceof NoLoginException){
            ModelAndView mv = new ModelAndView("redirect:/index");
            return mv;
        }

        ModelAndView mv = new ModelAndView();
        mv.setViewName("");
        mv.addObject("code",400);
        mv.addObject("msg","系统异常，请稍后再试...");
        if(handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            ResponseBody responseBody = handlerMethod.getMethod().getDeclaredAnnotation(ResponseBody.class);
            if(null==responseBody){
                if(ex instanceof ParamsException){
                    ParamsException pe =(ParamsException)ex;
                    mv.addObject("code",pe.getCode());
                    mv.addObject("msg",pe.getMsg());
                }
                return mv;
            }else{
                ResultInfo resultInfo = new ResultInfo();
                resultInfo.setCode(300);
                resultInfo.setMsg("系统异常，请重试！");
                if(ex instanceof  ParamsException){
                    ParamsException pe = (ParamsException)ex;
                    resultInfo.setCode(pe.getCode());
                    resultInfo.setMsg(pe.getMsg());
                }
                response.setContentType("application/json;charset=utf-8");
                PrintWriter out = null;
                try{
                    out=response.getWriter();
                    out.write(JSON.toJSONString(resultInfo));
                    out.flush();
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    if(out!=null){
                        out.close();
                    }
                }
                return null;
            }
        }
        return mv;
    }

}
